<?php
/**
 * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

/**
* Created by Echa
* Email: onemzj@163.com
* Date: 2016/8/2
* Time: 14:27
*/

/*API加密算法
 * $token  加密后的字符串
 * $encr_str 约定加密串
 * $time  传递当前时间
 * $expiration_time 有效时间  默认 10分钟
 * */
function apiencryption($mac='',$encr_str='key4token',$time='',$expiration_time=600){
	$maxtime=strtotime(date('Y-m-d H:i:s',$time+$expiration_time));
	$str=md5($encr_str.$time);
	$time=strtotime(date('Y-m-d H:i:s',time()));
	if($time<$maxtime){
		if($mac==$str){
			return true;
		}else{
			return 2;
		}
	}else{
		return false;
	}
	
}


function get_access_token($appid,$appsecret){
	$time = time();
    $ranstr = "ABCDEFGHIJKLMNOPQRSTUVWXYJ0123456789";
    $signstr = "";
    for ($i=0;$i<16;$i++){
        $signstr.= substr($ranstr, rand(0, strlen($ranstr)-1),1);
    }

    $token = md5(md5($appid.$appsecret.$time).$signstr);

    $result['expires_in'] = 7200;
    $result['token'] = $token;
    S('access_token_'.$appid,$result,$result['expires_in']);
    $_SESSION['access_token_'.$appid] = $result;
    return $result;
}

/*检查是否手机端*/
function ismobile() {
  // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
  if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
    return true;
   
  //此条摘自TPM智能切换模板引擎，适合TPM开发
  if(isset ($_SERVER['HTTP_CLIENT']) &&'PhoneClient'==$_SERVER['HTTP_CLIENT'])
    return true;
  //如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
  if (isset ($_SERVER['HTTP_VIA']))
    //找不到为flase,否则为true
    return stristr($_SERVER['HTTP_VIA'], 'wap') ? true : false;
  //判断手机发送的客户端标志,兼容性有待提高
  if (isset ($_SERVER['HTTP_USER_AGENT'])) {
    $clientkeywords = array(
      'nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile'
    );
    //从HTTP_USER_AGENT中查找手机浏览器的关键字
    if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
      return true;
    }
  }
  //协议法，因为有可能不准确，放到最后判断
  if (isset ($_SERVER['HTTP_ACCEPT'])) {
    // 如果只支持wml并且不支持html那一定是移动设备
    // 如果支持wml和html但是wml在html之前则是移动设备
    if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
      return true;
    }
  }
  return false;
 }
<<<<<<< .mine

/**
 * @role 红包开抢需要提前读缓存的页面
 * @author Jianhe
 * @return array
 */
function InfluenceRedWarPage(){
	return array(
		'Ad/ad_list'=>'',
		//'H5user/user_login'=>'uid',
		//'Wechat/getJsSign'=>'uid',
		'Users/my_address_default'=>'uid',
		'Community/is_show',
		'Users/my_address_details'=>'uid',
		//'NewCart/my_shopping_goods'=>'uid',
		'Users/users_location'=>'uid',
		//'BehaviorAddress/add'=>'',
		'Search/get_search_hot_keywords'=>'uid',
		'Message/my_message_state'=>'uid',
		'nav/get_nav'=>'',
		'HomeTemplate/get_home_templates'=>'group_id,page',
		'H5user/show_user_guide'=>'uid',
		//'BserBehavior/add_browse'=>'',
		'Seminar/get_seminar_template_page'=>'seminar_id,uid',
		'Category/get_categories'=>''
	);
}

/**
 * @role 红包开抢前需要提前写缓存的页面
 * @author Jianhe
 * @param $data
 */
function redWarIsWriteCache($data){
	$redWarTime=S('redWarTime');
	$InfluenceRedWarPage=InfluenceRedWarPage();
	$jdata=json_decode($data,true);
	if(array_key_exists(CONTROLLER_NAME.'/'.ACTION_NAME,$InfluenceRedWarPage)){
		//if($redWarTime && $_REQUEST['uid']==50115 && array_key_exists(CONTROLLER_NAME.'/'.ACTION_NAME,$InfluenceRedWarPage) && $redWarTime-600<time() && time()<$redWarTime+1200){
		$postParam=$InfluenceRedWarPage[CONTROLLER_NAME.'/'.ACTION_NAME];

		$postParamStr='';
		if(!empty($postParam)){
			$postParamArr=explode(",",$postParam);
			foreach($postParamArr as $li){
				$postParamStr.=$_REQUEST[$li];
			}
		}

		$cTime=300;
		if(time()>mktime(23,45) && time()<mktime(23,59)){
			$cTime=2100;
		}

		S('redWarReadPage_'.CONTROLLER_NAME.'_'.ACTION_NAME.$postParamStr,serialize($data),$cTime);
	}
}

/**
 * @role 红包开抢时读取页面的缓存
 * @author Jianhe
 * @return array
 */
function redWarIsReadCache(){
	$redWarTime=S('redWarTime');

	$InfluenceRedWarPage=InfluenceRedWarPage();
	if(array_key_exists(CONTROLLER_NAME.'/'.ACTION_NAME,$InfluenceRedWarPage)){
		//if($redWarTime && array_key_exists(CONTROLLER_NAME.'/'.ACTION_NAME,$InfluenceRedWarPage) && $redWarTime-300<time() && time()<$redWarTime+1200){
		$postParam=$InfluenceRedWarPage[CONTROLLER_NAME.'/'.ACTION_NAME];

		$postParamStr='';
		if(!empty($postParam)){
			$postParamArr=explode(",",$postParam);
			foreach($postParamArr as $li){
				$postParamStr.=$_REQUEST[$li];
			}
		}
		//S('redWarReadPage_'.CONTROLLER_NAME.'_'.ACTION_NAME,NULL);
		$dataJson=S('redWarReadPage_'.CONTROLLER_NAME.'_'.ACTION_NAME.$postParamStr);
		if($dataJson){

			if(CONTROLLER_NAME.'/'.ACTION_NAME=='Seminar/get_seminar_template_page'){
				$dataArr=json_decode(unserialize($dataJson),true);

				if($dataArr['status']==1){
					if(!empty($dataArr['seminar_list'])){
						foreach($dataArr['seminar_list'] as $seminar_key=>$seminar){
							if($seminar['type']==9 && !empty($seminar['bag_data'])){
								$UserBonus=new \SDXApi\Service\UserBonusService();
								foreach($seminar['bag_data'] as $key=>$val){
									$dataArr['seminar_list'][$seminar_key]['bag_data'][$key]['remainder_number']=$UserBonus->getRemainderNumber($val['type_id']);
									$dataArr['seminar_list'][$seminar_key]['bag_data'][$key]['surplus_time']=$val['send_start_date']>time()?$val['send_start_date']-time():0;
								}
							}
						}
					}
				}

				echo json_encode($dataArr);
				exit();

			}
			echo unserialize($dataJson);
			exit();
		}

	}
}
||||||| .r5736
=======

/**
 * @role 红包开抢需要提前读缓存的页面
 * @author Jianhe
 * @return array
 */
function InfluenceRedWarPage(){
	return array(
		'Ad/ad_list'=>'is_app,position_id,page',
		//'H5user/user_login'=>'uid',
		//'Wechat/getJsSign'=>'uid',
		'Users/my_address_default'=>'uid',
		'Community/is_show',
		'Users/my_address_details'=>'uid',
		//'NewCart/my_shopping_goods'=>'uid',
		'Users/users_location'=>'uid',
		//'BehaviorAddress/add'=>'',
		'Search/get_search_hot_keywords'=>'uid',
		'Message/my_message_state'=>'uid',
		'nav/get_nav'=>'',
		//'HomeTemplate/get_home_templates'=>'group_id,page,wid',
		'H5user/show_user_guide'=>'uid',
		//'BserBehavior/add_browse'=>'',
		'Seminar/get_seminar_template_page'=>'seminar_id,uid,wid,page',
		'Category/get_categories'=>'',
		//'Opensearch/lists'=>'cat_id,page,wid'
	);
}

/**
 * @role 红包开抢前需要提前写缓存的页面
 * @author Jianhe
 * @param $data
 */
function redWarIsWriteCache($data){
	$redWarTime=S('redWarTime');
	$InfluenceRedWarPage=InfluenceRedWarPage();
	$jdata=json_decode($data,true);
	if(array_key_exists(CONTROLLER_NAME.'/'.ACTION_NAME,$InfluenceRedWarPage)){
	//if($redWarTime && $_REQUEST['uid']==50115 && array_key_exists(CONTROLLER_NAME.'/'.ACTION_NAME,$InfluenceRedWarPage) && $redWarTime-600<time() && time()<$redWarTime+1200){
		$postParam=$InfluenceRedWarPage[CONTROLLER_NAME.'/'.ACTION_NAME];

		$postParamStr='';
		if(!empty($postParam)){
			$postParamArr=explode(",",$postParam);
			foreach($postParamArr as $li){
				$postParamStr.='_'.$_REQUEST[$li];
			}
		}

        $cTime = getWriteCacheTime();

		S('redWarReadPage_'.CONTROLLER_NAME.'_'.ACTION_NAME.$postParamStr,serialize($data),$cTime);
	}
}

/**
 * 获取缓存时间
 * @author Min
 * @return false|int
 */
function getWriteCacheTime() {
    $cTime=30;
    if(time()>mktime(23,50) && time()<mktime(23,59)){
        $cTime=mktime(23,59,59)-time()+1;
    }
    if(time()>mktime(00,00) && time()<mktime(00,01)){
        $cTime=mktime(00,01)-time();
    }
    if(time()>mktime(00,01) && time()<mktime(00,02)){
        $cTime=mktime(00,02)-time();
    }
    if(time()>mktime(00,02) && time()<mktime(00,03)){
        $cTime=300;
    }

    return $cTime;
}

/**
 * @role 红包开抢时读取页面的缓存
 * @author Jianhe
 * @return array
 */
function redWarIsReadCache(){
	$redWarTime=S('redWarTime');

	$InfluenceRedWarPage=InfluenceRedWarPage();
	if(array_key_exists(CONTROLLER_NAME.'/'.ACTION_NAME,$InfluenceRedWarPage)){
	//if($redWarTime && array_key_exists(CONTROLLER_NAME.'/'.ACTION_NAME,$InfluenceRedWarPage) && $redWarTime-300<time() && time()<$redWarTime+1200){
		$postParam=$InfluenceRedWarPage[CONTROLLER_NAME.'/'.ACTION_NAME];

		$postParamStr='';
		if(!empty($postParam)){
			$postParamArr=explode(",",$postParam);
			foreach($postParamArr as $li){
				$postParamStr.='_'.$_REQUEST[$li];
			}
		}
		//S('redWarReadPage_'.CONTROLLER_NAME.'_'.ACTION_NAME,NULL);
		$dataJson=S('redWarReadPage_'.CONTROLLER_NAME.'_'.ACTION_NAME.$postParamStr);
		if($dataJson){

			if(CONTROLLER_NAME.'/'.ACTION_NAME=='Seminar/get_seminar_template_page'){
				$dataArr=json_decode(unserialize($dataJson),true);

				if($dataArr['status']==1){
					if(!empty($dataArr['seminar_list'])){
						foreach($dataArr['seminar_list'] as $seminar_key=>$seminar){
							if($seminar['type']==9 && !empty($seminar['bag_data'])){
								$UserBonus=new \SDXApi\Service\UserBonusService();
								foreach($seminar['bag_data'] as $key=>$val){
									$dataArr['seminar_list'][$seminar_key]['bag_data'][$key]['remainder_number']=$UserBonus->getRemainderNumber($val['type_id']);
									$dataArr['seminar_list'][$seminar_key]['bag_data'][$key]['surplus_time']=$val['send_start_date']>time()?$val['send_start_date']-time():0;
								}
							}
						}
					}
				}

				echo json_encode($dataArr);
				exit();

			}
			echo unserialize($dataJson);
			exit();
		}

	}
}
>>>>>>> .r5778
 
 /*返回正确的API接口数据*/
 function returnApiSuccess($data=array(),$msg=null,$tips_msg=null){
 	$data['status']=1;
 	$data['error_msg']=$msg;
 	$data['tips_msg']=$tips_msg;
<<<<<<< .mine
 	$dataJson=json_encode($data);
	 redWarIsWriteCache($dataJson);
	echo $dataJson;
	exit;
||||||| .r5736
 	echo json_encode($data);
 	exit;
=======
	 $dataJson=json_encode($data);
	 redWarIsWriteCache($dataJson);
	 echo $dataJson;
 	exit;
>>>>>>> .r5778
 }
/*返回确认的API接口数据*/
function returnApiConfirm($data=array(),$msg=null){
    $data['status']=2;
    $data['error_msg']=$msg;
    echo json_encode($data);
    exit;
}
function return_data($data=array(),$page_size=null,$page_all=1,$record_all=null,$page_current=1,$msg=null){
	$return_data['status']=1;	//成功标记
	$return_data['error_msg']=$msg;	//描述文本
	$return_data['page_size']=$page_size;	//一页多少条数据
	$return_data['page_all']=$page_all;		//一共多少页
	$return_data['record_all']=$record_all;		//一共多少条数据
	$return_data['page_current']=$page_current;	//当前第几页
	$return_data['data']=$data;	//具体数据
	echo json_encode($return_data);
	exit;
}
 
 /*返回错误的API接口信息*/
 function returnApiError($msg=null,$res=array()){
 	$res['status']=0;
 	$res['error_msg']=$msg;
<<<<<<< .mine
	 $dataJson=json_encode($res);
	 redWarIsWriteCache($dataJson);
	 echo $dataJson;
||||||| .r5736
 	echo json_encode($res);
=======
	echo json_encode($res);
>>>>>>> .r5778
 	exit;
 }
 
 /*调用接口出现异常返回*/
 function returnApiErrorExample($e='当前系统繁忙，请稍后重试！'){
   $res = array(
    'status' => '0',
    'error_msg' => $e,
  );
  echo json_encode($res);
  exit;
}

 /*
	 * 数据去重复并重新排序
	 */
 function arrayUnique($array){
		$out = array();
		foreach ($array as $key=>$value) {
			if (!in_array($value, $out))
			{
				$out[$key] = $value;
			}
		}
		sort($out);    //重新排序
		return $out;
 }

 /*随机生成 5 个字母的字符串*/
 function getRndstr($length=5){
		$str='abcdefghijklmnopqrstuvwxyz';
		for($i=0;$i<$length;$i++){
			$rndcode=rand(0,25);
			$rndstr.=$str[$rndcode];
		}
		return $rndstr;
}

/*随机生成 6 位数字短信码*/
function getSmsCode($length=6){

	return str_pad(mt_rand(0, pow(10, $length) - 1), $length, '0', STR_PAD_LEFT);
}

/**
	 * POST 请求
	 * @param string $url
	 * @param array $param
	 * @param boolean $post_file 是否文件上传
	 * @return string content
	 */
 function http_post($url,$param,$post_file=false){
		$oCurl = curl_init();
		if(stripos($url,"https://")!==FALSE){
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
			curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
		}
		if (is_string($param) || $post_file) {
			$strPOST = $param;
		} else {
			$aPOST = array();
			foreach($param as $key=>$val){
				$aPOST[] = $key."=".urlencode($val);
			}
			$strPOST =  join("&", $aPOST);
		}
		curl_setopt($oCurl, CURLOPT_URL, $url);
		curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt($oCurl, CURLOPT_POST,true);
		curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
		$sContent = curl_exec($oCurl);
		$aStatus = curl_getinfo($oCurl);
		curl_close($oCurl);
		if(intval($aStatus["http_code"])==200){
			return $sContent;
		}else{
			return false;
		}
}

/**
	 * GET 请求
	 * @param string $url
	 */
function http_get($url){
		$oCurl = curl_init();
		if(stripos($url,"https://")!==FALSE){
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
			curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
		}
		curl_setopt($oCurl, CURLOPT_URL, $url);
		curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
		$sContent = curl_exec($oCurl);
		$aStatus = curl_getinfo($oCurl);
		curl_close($oCurl);
		if(intval($aStatus["http_code"])==200){
			return $sContent;
		}else{
			return false;
		}
}
/**
 * 写入本地记录
 * @param null $filename
 * @param $text
 */
function w_log($filename=null,$text){
	$open=fopen($filename.".txt","a" );
	fwrite($open,"[".date("Y-m-d H:i:s")."] ".$text."\r\n");
	fclose($open);
}


/*
 * 分页方式
 * $count   总记录数
 * $page  当前页码
 * $pagesize  每页显示条数
 * */
function show_page($count,$page,$pagesize=10){
    	$pagecount  = ceil($count/$pagesize); 
        if($page<=1 || $page==''){
        	$page=1;
        }
        if(!empty($pagecount) && $page>=$pagecount){
        	$page=$pagecount;
        }
        $first_page=$pagesize * ($page - 1);

        if($page>=$pagecount){
            $is_bottom = 1;
        }else{
            $is_bottom = 0;
        }
        $page=array('totle'=>$count, 'pagecount'=>$pagecount,'firstpage'=>$first_page,'pagesize'=>$pagesize,'is_bottom'=>$is_bottom);

        return $page;
}

/*
 * 根据指定详细获取经纬度
 * */
function get_lng_lat($address='',$city="广州"){
		/*$url='http://apis.map.qq.com/ws/geocoder/v1/?address='.$address.'&key=RFDBZ-BJL3F-463JB-J5VHW-Z2QDV-GQFV6';
		$data=json_decode(http_get($url),true);
		if($data){
			return array('lat'=>$data['result']['location']['lat'],'lng'=>$data['result']['location']['lng']);
		}else{
			return false;
		}*/
	    $data=address_to_gps_amap($address,$city);
	    if($data){
	    	return array('lat'=>$data['longitude'],'lng'=>$data['latitude']);
	    }else{
	    	return false;
	    }
	    
}


/**
 * 地址转GPS坐标值 百度版
 * @param $address
 * @return mixed
 */
function address_to_gps($address){
	//百度版
	//$url = "http://api.map.baidu.com/geocoder/v2/?address=".$address."&output=json&ak=ywp4UATuZpsOrrb8oHHSWQAwg9GMME0p";
    if(strpos($address,"广东工业大学(龙洞校区)")){
        $address = str_replace('广东工业大学(龙洞校区)', '广东工业大学(迎龙路)', $address);
    }else{
        $address = preg_replace('/\(.*?\)/', '', $address);
    }
    //特殊处理以下地址
	$address = str_replace('741号大院','741号',$address);
	$address = str_replace('新闻中心','',$address);
	$address = str_replace('南国花园', '南国一街', $address);
	//$address = str_replace('黄埔区', '', $address);

	$url = C('BAIDU_URL')."address=".$address."&output=json&ak=".C('BAIDU_KEY');
//    if($_POST['uid']==1980){
//        dump($url);
////        dump($address['all_address']);
////        dump($building_address_gps);
////        dump($address['building_address']);
//    }
	//dump($url);
	$get_data = file_get_contents($url);
	$Json_Data = json_decode($get_data, true);
	$data['latitude'] = $longitude =$Json_Data['result']['location']['lng'];    //经度   大
	$data['longitude'] = $latitude =$Json_Data['result']['location']['lat'];     //纬度  小

    if($data['longitude']==23.10871181423915){
        //黄埔区地址超区地点,定位到这个点 定义为超区经纬度
        $data['latitude'] = 113.638626;
        $data['longitude'] = 23.249706;
    }



	return $data;
}
/**
 * 地址转GPS坐标值 腾讯版
 * @param $address
 * @return mixed
 */
function address_to_gps_tencent($address){
    //腾讯版
    $url = "http://apis.map.qq.com/ws/geocoder/v1/?key=RFDBZ-BJL3F-463JB-J5VHW-Z2QDV-GQFV6&address=".$address;
    $get_data = file_get_contents($url);
    $Json_Data = json_decode($get_data, true);
    $data['latitude'] = $longitude =$Json_Data['result']['location']['lng'];    //经度
    $data['longitude'] = $latitude =$Json_Data['result']['location']['lat'];     //纬度
    return $data;
}

/**
 * 地址转GPS坐标值 高德版
 * @param $address
 * @param $city
 * @return mixed
 */
function address_to_gps_amap($address,$city="广州"){
    //高德版
    if(empty($city)){
        $city="广州";
    }
    $url = "http://restapi.amap.com/v3/geocode/geo?key=".C('AMAP_KEY')."&city=".$city."&address=".$address;

    $get_data = file_get_contents($url);
    $Json_Data = json_decode($get_data, true);

    $location = $Json_Data['geocodes'][0]['location'];

    $location_array = explode(",",$location);
    $data['latitude'] = $location_array[0];    //经度
    $data['longitude'] = $location_array[1];     //纬度
    //if($_POST['uid']==1980){
        //$data =GCJ02_To_BD09($data['latitude'],$data['longitude']);
//    }else{
//        $data = Convert_GCJ02_To_BD09($data['latitude'],$data['longitude']);
//    }
    return $data;
}

function GCJ02_To_BD09($longitude,$latitude){
    $url = "http://api.map.baidu.com/geoconv/v1/?coords=".$longitude.",".$latitude."&from=3&to=5&ak=".C('BAIDU_KEY');
    $get_data = file_get_contents($url);
    $Json_Data = json_decode($get_data, true);
    $data['latitude'] = $longitude =$Json_Data['result'][0]['x'];    //经度   大
    $data['longitude'] = $latitude =$Json_Data['result'][0]['y'];     //纬度  小
    return $data;
}


/**
 * GPS坐标值转地址
 * Author:何锐 <poptser@foxmail.com>
 * @param $address
 */
function gps_to_address($longitude,$latitude){
	//腾讯版
//        $url ="http://apis.map.qq.com/ws/geocoder/v1/?location=".$longitude.",".$latitude."&get_poi=0&key=".C('TENCENT_KEY');
//        $get_data = file_get_contents($url);
//        $Json_Data = json_decode($get_data, true);
//        $data['all_address']  =$Json_Data['result']['address'];    //详细地址
//        $data['lit_address']  =$Json_Data['result']['address_component']['street_number'];     //简要地址
//        return $data;
	//百度版
	$url =C('BAIDU_URL')."ak=".C('BAIDU_KEY')."&coordtype=wgs84ll&location=".$latitude.",".$longitude."&output=json";
	$get_data = file_get_contents($url);
	$Json_Data = json_decode($get_data, true);
	$data['all_address']  =$Json_Data['result']['formatted_address'];    //详细地址
	$data['lit_address']  =$Json_Data['result']['sematic_description'];     //简要地址
	return $data;
}

/*
 * 食得鲜发送短信接口
 * message_code  //order_finish 订单完成    order_distribution 订单开始配送   order_pay_ok  支付完成   ver_code 验证码  order_refund 用户退款
 * return  array('state'=>1);发送成功！ 或者 array('state'=>0);发送失败！ 
 * 
 * 
 * */
function sdx_sendsms($send=array()){
	$url = C('SDX_SEND_DOMIANURL')."index.php?m=default&c=Smsapi&a=send_message_url";
	$data['message_code'] =  $send['messcode'];					//短信模板编码
	$data['user_phone'] = $send['mobile'];						//需发送的手机号
	$data['user_name'] =  $send['consignee'];		//用户姓名
	$data['user_rank'] =  $send['user_rank'];
	$data['order_sn'] =  $send['order_sn'];						//订单号
	$data['ver_code'] = $send['ver_code'] ;						//验证码时填写
	$data['user_id'] =  $send['user_id'];			//用户ID
	$post['data'] = json_encode($data);
	$res=json_decode(http_post($url,$post),true);
	if($res['state']==1){
		return true;
	}else{
		return false;
	}
}

/**
 * 获取所有输入转JSON
 * Author:何锐 <poptser@foxmail.com>
 * @param bool $array	true array   false obj
 * @return bool|mixed
 */
function get_all_data_by_json($array =true){
	//$get_data= file_get_contents("php://input");
	$get_data= I("post.data");
	if($array){
		$array_data = json_decode($get_data,true);
	}else{
		$array_data = json_decode($get_data);
	}
	if ($array_data){
		return $array_data;
	}else{
		return false;
	}

}

/**
 * 获取商品库存缓存
 * @param $goods_id 商品id
 * @param $wid  仓库id
 * @param $goods_practical_num  商品最新库存
 * @return mixed
 */
function get_goods_practical_num_cache($goods_id,$wid,$goods_practical_num){
    //获取库存缓存
    $cache_goods_practical_num = S('goods_practical_num_cache_'.$goods_id.'_'.$wid);
    if($cache_goods_practical_num===false){
        S('goods_practical_num_cache_'.$goods_id.'_'.$wid,$goods_practical_num);
        $cache_goods_practical_num = $goods_practical_num;
    }
    return $cache_goods_practical_num;
}

/**
 * 设置商品库存缓存
 * @param $goods_id 商品id
 * @param $wid  仓库id
 * @param $goods_practical_num  商品最新库存
 */
function set_goods_practical_num_cache($goods_id,$wid,$goods_practical_num){
//    $goods_practical_num = $goods_practical_num>0 ? $goods_practical_num : 0;
    S('goods_practical_num_cache_'.$goods_id.'_'.$wid,$goods_practical_num);
}

/**
 * 更新商品库存缓存
 * @param $goods_id
 * @param $wid
 * @param $num
 * @param string $op
 */
function update_goods_practical_num_cache($goods_id, $wid, $num, $op = 'add')
{
    //获取库存缓存
    $cache_goods_practical_num = S('goods_practical_num_cache_' . $goods_id . '_' . $wid);
    if ($cache_goods_practical_num === false) {
        $goodsstorage = D('Goodsstorage')->get_warehous_goods($wid, $goods_id);
        if (!empty($goodsstorage)) {
            $cache_goods_practical_num = $goodsstorage['practical_num'];
        } else {
            $cache_goods_practical_num = 0;
        }
    }

    //获取队列库存缓存
    $cache_queue_goods_stock = get_queue_goods_stock($goods_id,$wid);
    if ($op == 'add') {
        set_goods_practical_num_cache($goods_id, $wid, $cache_goods_practical_num + $num);
        set_queue_goods_stock($goods_id,$wid,$cache_queue_goods_stock + $num);
    } elseif ($op == 'dec') {
        set_goods_practical_num_cache($goods_id, $wid, $cache_goods_practical_num - $num);
        set_queue_goods_stock($goods_id,$wid,$cache_queue_goods_stock - $num);
    }
}

/**
 * 获取商品队列库存缓存
 * @param $goods_id
 * @param $wid
 * @return mixed
 */
function get_queue_goods_stock($goods_id,$wid){
    $goods_stock = S('queue_goods_stock_'.$goods_id.'_'.$wid);
    if(empty($goods_stock)){
        $goods_stock = S('goods_practical_num_cache_'.$goods_id.'_'.$wid);
        S('queue_goods_stock_'.$goods_id.'_'.$wid,$goods_stock);
    }
    return $goods_stock;
}

/**
 * 设置商品队列库存缓存
 * @param $goods_id
 * @param $wid
 * @param $num
 */
function set_queue_goods_stock($goods_id,$wid,$num){
    S('queue_goods_stock_'.$goods_id.'_'.$wid,$num);
}

function array_orderby(){
    $args = func_get_args();
    $data = array_shift($args);
    foreach ($args as $n => $field) {
        if (is_string($field)) {
            $tmp = array();
            foreach ($data as $key => $row)
                $tmp[$key] = $row[$field];
            $args[$n] = $tmp;
        }
    }
    $args[] = &$data;
    call_user_func_array('array_multisort', $args);
    return array_pop($args);
}

/**
 * 断点调试，打印数组
 * @param $data
 */
function dd($data){
    print_r($data);
    die;
}


		